package com.shundr.common;

import java.util.ArrayList;
import java.util.List;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.shundr.R;
import com.shundr.base.BaseActivity;
import com.shundr.cargo.CargoDetailsActivity;
import com.shundr.common.adapter.FavoriteAdapter;
import com.shundr.common.http.CommonRequest;
import com.shundr.common.model.FavoriteInfo;
import com.shundr.common.util.CommonConfig;
import com.shundr.common.util.ProgressDialogHelper;
import com.shundr.common.util.ToastHelper;
import com.shundr.common.view.CListView;
import com.shundr.common.view.CListView.OnLoadMoreListener;
import com.shundr.common.view.CListView.OnRefreshListener;
import com.shundr.common.view.DialogDelete;
import com.shundr.common.view.DialogDelete.onDeleteListener;
import com.shundr.frame.constant.LContext;
import com.shundr.frame.http.LHttpRequest;
import com.shundr.truck.MyTruckDetailActivity;

public class FavoriteListActivity extends BaseActivity {

	private static final int PAGE_SIZE = LContext.PAGE_SIZE;
	private int pageNo = 1;
    
	private List<FavoriteInfo> mList = new ArrayList<FavoriteInfo>();

	private FavoriteAdapter mAdapter;
	
	private CListView mListView;
	private RelativeLayout mLayoutNoContent;
	private TextView tvNoContent;
	
	private Handler mHandler = new Handler(){

		@Override
		public void handleMessage(Message msg) {
			try {
				switch (msg.what) {
				case CommonRequest.GET_FAVORITE_LIST_SUCCEED:
					List<FavoriteInfo> infos = (List<FavoriteInfo>)msg.obj;
					if (infos != null) {
						if (infos.size() > 0) {
							if (pageNo == 1) {
								mList.clear();
								mListView.setVisibility(View.VISIBLE);
								mLayoutNoContent.setVisibility(View.GONE);
								if (infos.size() >= PAGE_SIZE) {
									mListView.setCanLoadMore(true);
									mListView.setAutoLoadMore(true);
									mListView.setCanRefresh(true);
								} else {
									mListView.setCanLoadMore(false);
									mListView.setAutoLoadMore(false);
									mListView.setCanRefresh(false);
								}
							}
							mList.addAll(infos);
							mAdapter.notifyDataSetChanged();
							if (pageNo == 1) {
								mListView.setSelection(0);
							}
							pageNo = pageNo + 1;
						} else {
							if (pageNo == 1) {
								mListView.setVisibility(View.GONE);
								mLayoutNoContent.setVisibility(View.VISIBLE);
							} else {
								ToastHelper.toastLong(mContext, "最后一页");
							}
						}
					}
					ProgressDialogHelper.dismiss();
					mListView.onRefreshComplete();
					mListView.onLoadMoreComplete();
					break;
				case LHttpRequest.REQUEST_FAILED:
					mListView.onLoadMoreComplete();
					mListView.onRefreshComplete();
					ProgressDialogHelper.dismiss();	
					ToastHelper.toastShort(mContext, msg.obj.toString());
					break;

				default:
					break;
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
			super.handleMessage(msg);
		}
		
	};
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_car_list);
		mListView = (CListView) findViewById(R.id.lv_list);
		mLayoutNoContent = (RelativeLayout) findViewById(R.id.layout_no_content);
		tvNoContent = (TextView) findViewById(R.id.tv_no_content);
		tvNoContent.setText("您还没有收藏");
		mAdapter = new FavoriteAdapter(mContext,mList);
		mListView.setAdapter(mAdapter);
		mListView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				Intent intent = null;
				FavoriteInfo info = mList.get(position - 1);
				switch (info.getObjectType()) {
				case CommonConfig.OPERATE_OBJECT_CARGO:
					intent = new Intent(mContext, CargoDetailsActivity.class);
					intent.putExtra("CargoId", info.getObjectId() + "");
					break;
				case CommonConfig.OPERATE_OBJECT_TRUCKER:
					intent = new Intent(mContext, MyTruckDetailActivity.class);
					intent.putExtra("id", info.getObjectId());
					break;

				default:
					break;
				}
				startActivity(intent);
			}
		});
		
		mListView.setOnItemLongClickListener(new OnItemLongClickListener() {

			@Override
			public boolean onItemLongClick(AdapterView<?> parent, View view,
					final int position, long id) {
				FavoriteInfo info = mList.get(position - 1);
				if(info!=null){
					final int objectId=info.getObjectId(); 
					final int objectType=info.getObjectType();
					DialogDelete deleteFav=new DialogDelete(mContext);
					deleteFav.setOnDeleteListener(new onDeleteListener() {
						
						@Override
						public void delete() {
							new CommonRequest(mContext, new Handler(){

								@Override
								public void handleMessage(Message msg) {
									super.handleMessage(msg);
									switch (msg.what) {
									case CommonRequest.DELETE_FAVORITE_SUCCEED:
										ToastHelper.toastShort(mContext, "删除成功");
										mList.remove(position-1);
										mAdapter.notifyDataSetChanged();
										if(mList.size() == 0){
											mListView.setVisibility(View.GONE);
											mLayoutNoContent.setVisibility(View.VISIBLE);
										}
										break;
									case LHttpRequest.REQUEST_FAILED:
						                ToastHelper.toastShort(mContext, msg.obj.toString());
										break;
									default:
										break;
									}
								}
								
							}).deleteFavorite(objectId,objectType);
						}
					});
					deleteFav.show();
				}
				return true;
			}
		});
		
		mListView.setOnRefreshListener(new OnRefreshListener() {
			
			@Override
			public void onRefresh() {
				pageNo = 1;
				new CommonRequest(mContext, mHandler).getFavoriteList(pageNo, PAGE_SIZE);
			}
		});
		mListView.setOnLoadListener(new OnLoadMoreListener() {
			@Override
			public void onLoadMore() {
				new CommonRequest(mContext, mHandler).getFavoriteList(pageNo, PAGE_SIZE);
			}
		});
		mListView.setCanLoadMore(false);
		mListView.setAutoLoadMore(false);
		mListView.setCanRefresh(true);
		ProgressDialogHelper.show(mContext, "正在加载中...");
		new CommonRequest(mContext, mHandler).getFavoriteList(pageNo, PAGE_SIZE);
	}
}
